package controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import model.Article;

import java.util.Date;
import java.util.List;

public class IndexController extends Controller {

    public void index(){
        List<Article> articles=new Article().findAll();
        setAttr("articles",articles);
        render("index.html");
    }

    public void detail(){
        int id=getParaToInt("id");
        Article data=new Article().findById(id);
        setAttr("data",data);
        render("detail.html");
    }

    public void add(){
        String title="标题"+Math.random()*1000;
        String content="内容"+Math.random()*1000;
        int click= (int) (Math.random()*1000);
        int view= (int) (Math.random()*1000);
        Date time=new Date();
        Article article=new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setClick(click);
        article.setView(view);
        article.setTime(time);
        renderText("添加数据的结果："+article.save());
    }

    public void update(){
        Article article=new Article().findById(1);
        article.setClick(200);
        renderText("修改的结果："+article.update());
    }

    public void delete(){
        Article article=new Article().findById(1);
        renderText("删除的结果:"+article.delete());
    }

    public void json(){
        Ret data=new Ret();
        data.set("test","this is ret");
        data.set("database",new Article().findAll());
        setAttr("jsonData1",data);
        setAttr("databaseToJson",new Article().findAll());
        renderJson();
    }

    public void form(){
        //获取表单数据
        String data=get("title");
        //数据处理
        setAttr("datas","前端数据："+data);
        renderJson();
    }

    public void table(){
        int pageNumber=getInt("page");
        int pageSize=getInt("limit");
        setAttr("datas",new Article().paginate(pageNumber,pageSize,"select *","from article"));
        renderJson();
    }
}
